package com.txf.other_playerlibrary.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.txf.other_playerlibrary.interfaces.IPlayerControl;
import com.txf.other_playerlibrary.interfaces.IPlayerControlView;
import com.txf.other_playerlibrary.ui.player.control.PlayerControlView;

/**
 * @author txf
 * @create 2019/2/14 0014
 * @
 */
public class PlayerView extends RelativeLayout implements IPlayerControl {
    IPlayerControlView mIPlayerControlView;
    PlayerSurfaceView mPlayerSurfaceView;
    /**
     * 1:开始播放
     * 2:暂停播放
     * 3:寻求播放新的位置
     */
    int state;//播放状态

    public PlayerView(Context context) {
        super(context);
        onCreate();
    }

    public PlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        onCreate();
    }

    public PlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        onCreate();
    }

    private void onCreate() {
        addSurfaceView();
        setPlayerControlView(new PlayerControlView(getContext()));
    }

    private void addSurfaceView() {
        addView(new PlayerSurfaceView(getContext()), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void setPlayerControlView(IPlayerControlView iPlayerControlView) {
        if (mIPlayerControlView != null) {
            removeView(mIPlayerControlView.getView());
        }
        mIPlayerControlView = iPlayerControlView;
        mIPlayerControlView.setIPlayerControl(this);
        //设置控制层背景 防止隐藏后 显示出来 不绘制
        mIPlayerControlView.getView().setBackgroundColor(0x01000000);
        addView(mIPlayerControlView.getView(), mIPlayerControlView.getViewLayoutParams());
    }

    @Override
    public void start() {
        state = 1;

    }

    @Override
    public void pause() {
        state = 2;

    }

    @Override
    public long getDuration() {
        return 0;
    }

    @Override
    public long getCurrentPosition() {
        return 0;
    }

    @Override
    public void seekTo(long pos) {
        state = 3;

    }

    @Override
    public int getState() {
        return state;
    }
}
